/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import Dao.EventoJpaController;
import Dao.HotelJpaController;
import Dao.exceptions.IllegalOrphanException;
import Dao.exceptions.NonexistentEntityException;
import Dao.exceptions.PreexistingEntityException;
import Logica.Evento;
import Logica.Hotel;
//import Logica.Pertenece;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author CORE i5
 */
public class ControladorHotel {

    EventoJpaController daoEvento;
    //PerteneceJpaController daoPertenece;
    private EntityManagerFactory factory;
    private ConnectionFactory connection;
    private HotelJpaController daoHotel;

    public ControladorHotel() {
        connection = ConnectionFactory.getFactory();
        factory = connection.getFactoria();
        daoHotel = new HotelJpaController(factory);
        daoEvento = new EventoJpaController(factory);
    }

    //Registrar deportista
    public int guardar(String nombre, String direccion, String telefono, String paginaWeb) {
        Hotel h = new Hotel();
        h.setIdHotel(obtenerNuevoId()+"");
        h.setNombre(nombre);
        h.setDireccion(direccion);
        h.setTelefono(telefono);
        h.setWeb(paginaWeb);

        try {
            daoHotel.create(h);
            return -1;
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int borrar(String id) {
        try {
            daoHotel.destroy(id);
            return -1;
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public void modificar(String codigo, String nombre, String direccion, String telefono, String paginaWeb) {
        Hotel h = daoHotel.findHotel(codigo);
        h.setNombre(nombre);
        h.setDireccion(direccion);
        h.setTelefono(telefono);
        h.setWeb(paginaWeb);

        try {
            daoHotel.edit(h);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Hotel consultar(String id) {
        Hotel p = daoHotel.findHotel(id);
        return p;
    }

    public List<Hotel> consultarTodo() {
        List<Hotel> hoteles = daoHotel.findHotelEntities();
        return hoteles;
    }

    public Collection<Evento> obtenerEventos(String id) {
        Hotel h = daoHotel.findHotel(id);
        Collection<Evento> eventos = h.getEventoCollection();
        return eventos;
    }

    public void agregarEvento(String idEvento, String idHotel) {
        Hotel h = daoHotel.findHotel(idHotel);
        Evento e = daoEvento.findEvento(idEvento);
        Collection<Evento> pertenece = h.getEventoCollection();
        h.setEventoCollection(pertenece);

        try {
            daoHotel.edit(h);
        } catch (Exception ex) {
            Logger.getLogger(ControladorHotel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private int obtenerNuevoId() {
        List<Hotel> hoteles = consultarTodo();
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < hoteles.size(); i++) {
            int current = Integer.parseInt(hoteles.get(i).getIdHotel());
            if (current > max) {
                max = current;
            }
        }
        if (max == Integer.MIN_VALUE) {
            max = 0;
        }
        return (max+1);
    }

}
